<?php

/*
 * This class will manage gathering rss and atom feeds of specified urls.
 */

class Search_Feeder {

    public function __construct() {
        
    }

    /**
     * This method will check whether given url supports RSS or ATOM.
     * @param type $link
     * @return boolean
     */
    private function isRss($link) {
        $isRss = false;
        $isAtom = false;
        $xml = simplexml_load_file($link);
        if ($xml->getName() == "feed") {
            $isAtom = true;
        } else {
            $isRss = true;
        }
        return $isRss;
    }

    private function sanitize($input) {
        return htmlentities(strip_tags($input));
    }

    public function generateNewSearchIndex($location, $feederUrls = array()) {
        $index = new Zend_Search_Lucene($location, true);
        foreach ($feederUrls as $urls) {
            $url = $urls['link'];
            $categoryArray = $urls['category'];
            $data = array();
            $channel = Zend_Feed::import($url);
            if ($this->isRss($url)) {
                foreach ($channel as $item) {
                    $data['title'] = $item->title();
                    $data['link'] = $item->link();
                    $data['description'] = $item->description();
                    $data['category'] = $categoryArray;
                    $document = $this->createNewDoc($data);
                    echo '<br>' . $data['title'] . '<hr>';
                    $index->addDocument($document);
                    echo '<pre>';
                    print_r($document);
                    echo '</pre>';
                }
            } else {
                foreach ($channel as $entry) {
                    $data['title'] = $entry->title();
                    $data['link'] = $entry->link('alternate');
                    $data['description'] = $entry->content();
                    $data['category'] = $categoryArray;
                    $document = $this->createNewDoc($data);
                    echo '<br>' . $data['title'] . '<hr>';
                    $index->addDocument($document);
                }
            }
        }
        $index->commit();
        echo $index->count() . " Document(s) indexed.<br>";
    }

    private function createNewDoc($data = array()) {
        if ($data['title'] && $data['link'] && $data['description']) {
            $document = new Zend_Search_Lucene_Document();
            $categoryArray = $data['category'];
            $catIndex = 1;
            foreach ($categoryArray as $catValue) {
                $document->addField(Zend_Search_Lucene_Field::keyword('category_' . $catIndex, $this->sanitize(strtolower($catValue))));
                $catIndex++;
            }
            $document->addField(Zend_Search_Lucene_Field::keyword('link', $this->sanitize($data['link'])));
            $document->addField(Zend_Search_Lucene_Field::text('title', $this->sanitize($data['title'])));
            $document->addField(Zend_Search_Lucene_Field::text('contents', $this->sanitize($data['description'])));
            return $document;
        }
    }

}
